package rs.networx.servlets;

import java.io.IOException;
import java.util.List;

import javax.ejb.EJB;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import rs.networx.dao.FriendRequestDao;
import rs.networx.dao.NottificationDao;
import rs.networx.dao.PersonDao;
import rs.networx.entity.Nottification;
import rs.networx.entity.Person;
import rs.networx.entity.Post;
import rs.networx.util.CleanUpDateOfBirth;

public class RegistredUserHomeServlet extends HttpServlet {

	private static final long serialVersionUID = 675143510789504200L;

	@EJB
	private PersonDao personDao;

	@EJB
	private FriendRequestDao friendRequestDao;

	@EJB
	private NottificationDao nottificationDao;

	private String username;
	private String password;
	private Person loggedUser;

	private Integer nottificationsCount;
	private Integer friendRequestsCount;

	private List<Post> loggedUsersOwnedPosts;

	private List<Nottification> nottifications;

	private String cleanedUpDateOfBirth;
	
	CleanUpDateOfBirth dateed = new CleanUpDateOfBirth();

	@SuppressWarnings("unchecked")
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws IOException, ServletException {

		System.out.println("LOADING: RegistredUserHomeServlet");

		response.setContentType("text/html; charset=UTF-8");
		request.setCharacterEncoding(response.getCharacterEncoding());

		// dobavljamo sesiju i objekte sa sesije...
		HttpSession session = request.getSession();

		loggedUser = (Person) session.getAttribute("logovaniUser");
		nottificationsCount = (Integer) session
				.getAttribute("nottificationsCount");
		friendRequestsCount = (Integer) session
				.getAttribute("friendRequestsCount");
		nottifications = (List<Nottification>) session
				.getAttribute("nottifications");

		try {
			username = loggedUser.getEmail();
			password = loggedUser.getPassword();
			System.out.println(username + "  " + password);

			System.out.println("nottificationsCount: " + nottificationsCount);
			System.out.println("nottifications: " + nottifications.size());

			System.out.println("friendRequestsCount: " + friendRequestsCount);
		} catch (Exception e) {
		}

		try {
			System.out.println((personDao.findById(loggedUser.getId()))
					.getDob());
			loggedUser
					.setDob((personDao.findById(loggedUser.getId())).getDob());
		} catch (Exception e1) {
		}

		try {
			// dobavljamo 10 postova za logovanog korisnika...
			loggedUsersOwnedPosts = personDao.getFirstTenOwnedPosts(loggedUser
					.getId());
			System.out.println("loggedUsersOwnedPosts: "
					+ loggedUsersOwnedPosts.size());

			// nottificationsCount bi trebao da bude isti kao i
			// nottifications.size()...
			System.out.println("nottificationsCount: " + nottificationsCount
					+ "  " + "nottifications: " + nottifications.size());

			if (nottificationsCount.equals(nottifications.size())) {
				// kada smo dobavili sve nonviewed notifikacije za logged usera,
				// njih stavljamo da su viewed...
				// i redirektujemo na registered users home page...
				for (int i = 0; i < nottifications.size(); i++) {
					nottifications.get(i).setViewed(true);
					nottificationDao.merge(nottifications.get(i));
				}
			}
		} catch (Exception e) {
		}

		// sada smo sredili sve nonviewed notifikacije sa sesije, pa da vidimo
		// da li ima nekih novih u bazi...
		// sada dobavljamo broj nonviewed notifikacija (ako ih ima) za datog
		// ulogovanog korisnika...
		nottificationsCount = nottificationDao
				.getNottificationsCountForLoggedUser(loggedUser);
		System.out.println("NOTIFICATIONS: " + nottificationsCount);

		// isto vazi i za neodgovorene zahteve za prijateljstvo...
		// sada dobavljamo neodgovorene zahteve za prijateljstvo (ako ih ima)...
		friendRequestsCount = friendRequestDao
				.getfriendRequestsCountForLoggedUser(loggedUser);
		System.out.println("FRIEND REQUSETS: " + friendRequestsCount);

		cleanedUpDateOfBirth = dateed.cleanUpDateOfBirth(loggedUser.getDob(), loggedUser);
		System.out.println(cleanedUpDateOfBirth);

		session.setAttribute("loggedUsersOwnedPosts", loggedUsersOwnedPosts);
		session.setAttribute("nottificationsCount", nottificationsCount);
		session.setAttribute("friendRequestsCount", friendRequestsCount);
		session.setAttribute("cleanedUpDateOfBirth", cleanedUpDateOfBirth);

		RequestDispatcher disp = request
				.getRequestDispatcher("ViewSelectedPostsPage.jsp");
		disp.forward(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws IOException, ServletException {
		doGet(request, response);
	}


}
